from datetime import datetime, timedelta

import akshare as ak

from core.Constants import UNIT_YI


# 获取股票的日K
def get_one_stock(code):
    yesterday = datetime.today() - timedelta(days=1)  # 先获取昨天
    start_date = (yesterday- timedelta(days=60)).strftime('%Y%m%d')  # 多留点余量
    end_date = yesterday.strftime('%Y%m%d')
    # 1. 获取历史行情
    df = ak.stock_zh_a_hist(symbol=code, period="daily", start_date= start_date,end_date= end_date, adjust="qfq")

    # 2. 计算多条均线（5、10、15、20）
    for n in [5, 10, 15, 20,30]:
        df[f'MA{n}'] = df['收盘'].rolling(window=n).mean()
    # 计算五日成交量均线
    df['CJ5'] = (df['成交额'].rolling(window=5).mean() / UNIT_YI).round(2)
    # 3. 计算 20 日布林带（示例，可同理换成 5/10/15）
    df['STD20'] = df['收盘'].rolling(window=20).std()
    df['BOLL_UPPER'] = df['MA20'] + 2 * df['STD20']
    df['BOLL_LOWER'] = df['MA20'] - 2 * df['STD20']

    return df

if __name__ == "__main__":
    aa = get_one_stock("603300")
    print(aa)

# TODO 时间日期设置

# https://quote.eastmoney.com/changes/stocks/sz000989.html